<template>
  <div class="mnt-dialog form-container">
    <el-form ref="form" :model="formModel" label-width="120px" :rules="rules">
      <el-row style="text-align: center">
        <el-col :span="12">
          <el-form-item label="工艺路线:" prop="tmBasRouteId">
            <el-select v-model="formModel.tmBasRouteId" @change="changeRoute" clearable filterable>
              <el-option v-for="item in routeOptions" :key="item.tmBasRouteId" :label="item.routeName" :value="item.tmBasRouteId"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="上线产线代码:" prop="lineNo">
            <el-input v-model="formModel.lineNo" disabled/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <hey-table ref="table" :table-data="tableData" @selection-change="handleSelectionChange" :hasPagination="false" :is-select="true" :columns="columns">

    </hey-table>
    <el-row class="submit-area">
      <el-col :span="24">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="handleSubmit()">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>

  export default {
    name: 'OrderMatchMnt',
    props: ['editData'],
    data() {
      return {
        routeOptions: [],
        matchList: [],
        tableData: [],
        formModel: {
          tmBasRouteId: '',
          lineNo: '',
        },
        columns: [
          { title: '计划编号', key: 'planNo'},
          { title: '订单编号', key: 'orderId'},
          { title: '车辆VIN号', key: 'vin'},
          { title: '订单状态', key: 'orderStatus',clcd:'order_status_cd'},
          { title: '工厂代码', key: 'plantNo'},
          { title: '工厂名称', key: 'plantNameC'},
          { title: '整车物料号', key: 'materialNo'},
          { title: '物料描述', key: 'materialDesc'},
          { title: '车型', key: 'productTypeKey',clcd:'key_product_type'},
          { title: '生产类型', key: 'proType',clcd:'Key_Product_Type'},
          { title: '销售类型', key: 'saleType',clcd:'Key_Sale_Type'},
          { title: '需求类型', key: 'needType',clcd:'need_type_cd'},
          { title: 'VIN年份', key: 'vinYear',clcd:'Key_Vin_ModeYear'},
          { title: '预分配经销商编号', key: 'dealerCode'},
          { title: '计划上线日期', key: 'planStartDate'},
          { title: '计划下线日期', key: 'planEndDate'},
          { title: '实际上线日期', key: 'practicalStartDate'},
          { title: '实际下线日期', key: 'practicalEndDate'},
          { title: '工艺路线代码', key: 'routeNo'},
          { title: '工艺路线名称', key: 'routeName'},
          { title: '上线产线代码', key: 'lineNo'},
          { title: '上线产线名称', key: 'lineNameC'},
          { title: '上线序列号', key: 'sortNum'},
          { title: '创建时间', key: 'createTime' },
          { title: '创建人', key: 'createUsername' },
          { title: '更新次数', key: 'updateNum'},
          { title: '最后修改时间', key: 'lastUpdateTime'},
          { title: '最后修改人', key: 'lastUpdateUsername' },
        ],
        rules: {}
      }
    },
    computed: {},
    mounted() {
      this.init()
      this.queryList()
    },
    methods: {
      queryList() {
        const param = {}
        param.isDelete = '0'
        param.orderStatus = '01'
        param.planNo = this.editData.planNo
        this.$refs.table.query('/tmOfmOrder/queryTmOfmOrderList', param, '', false)
      },
      handleSelectionChange(data){
        this.matchList = data
      },
      init() {
        this.$form = this.$refs.form
        const {editData} = this
        this.routeOptions = editData.routeOptions
        this.initRules()
      },
      initRules() {
        const REQUIRED_MSG = this.CONST.REQUIRED_MSG
        this.rules = {
          tmBasRouteId: [{required: true, message: REQUIRED_MSG, trigger: ['change', 'blur']}],
          lineNo: [{required: true, message: REQUIRED_MSG, trigger: ['change', 'blur']}],
        }
        this.$nextTick(_ => {
          this.$form.clearValidate()
        })
      },
      handleSubmit() {
        this.$refs.form.validate(valid => {
          if (valid) {
            if(this.matchList.length==0){
              this.$warn('请至少选中一条数据!')
              return false
            }
            const {formModel} = this
            const param = {}
            param.matchList = this.matchList
            for(let i=0;i<param.matchList.length;i++){
              if(this.matchList[i].planStartDate){
                param.vinList[i].planStartDate = new Date(Date.parse(this.matchList[i].planStartDate.replace(/-/g,"/")))
              }
              if(this.matchList[i].planEndDate){
                param.vinList[i].planEndDate = new Date(Date.parse(this.matchList[i].planEndDate.replace(/-/g,"/")))
              }
              param.matchList[i].createTime = ''
              param.matchList[i].lastUpdateTime = ''
              param.matchList[i].tmBasRouteId = formModel.tmBasRouteId
              param.matchList[i].lineNo = formModel.lineNo
            }
            const url = '/tmOfmOrder/orderMatch'
            const tip = '订单匹配成功!'
            this.post(url, param).then(res => {
              this.$info(tip, () => {
                this.$emit('mntSuc', res)
                this.closeDialog()
              })
            })
          }
        })
      },
      changeRoute(value){
        if(value){
          const obj = this.routeOptions.find(item => item.tmBasRouteId === value)
          this.formModel.lineNo = obj.lineNo
        }else{
          this.formModel.lineNo = ''
        }
      },
      closeDialog() {
        this.$parent.$emit('update:visible', false)
      }
    }
  }
</script>

<style lang="scss" scoped>

</style>
